require 'lib/utils.rb'

if(!ARGV[0] || !ARGV[1] || !ARGV[2] || !ARGV[3])
	puts "Run 'subtract.rb ratings.txt groupResults.txt finalResults.txt attendees.txt'"
	exit
end




ratings = getRatingsFromFile(ARGV[0]);

players = getPlayersFromGames(ARGV[1]);
players += getPlayersFromGames(ARGV[2]);
players.uniq!

$fileName = "attendees-" + (Date.today.to_s) + ".txt";
fout = File.new($fileName, "w")
players.each { |val|
	fout <<  val << "\n";
}
fout.close

players += getPlayersFromList(ARGV[3]).each { |val| val.chomp! };
players.uniq!

ratings.keys.each {|key|
	ratings[key]-=10.0 if(! players.member? key);		
}

$fileName = "ratings-" + (Date.today.to_s) + ".txt";

if(File.exist?($fileName))
	$i = 1;
	$fileName = "ratings-" + (Date.today.to_s) + "-" +  $i.to_s + ".txt";
	while(File.exist?($fileName))
		$i+=1;
		$fileName = "ratings-" + (Date.today.to_s) + "-" +  $i.to_s + ".txt";
	end	
end

devalvationCoefficient = 1.0

fout = File.new($fileName, "w")
ratings.sort{|a,b| b[1] <=> a[1]}.each {|key, value| 
	#if(devalvationCoefficient == 1.0 && value > 900)
	# devalvationCoefficient = 900.0/value;
	#end
	#value = value*devalvationCoefficient
	fout <<  key.to_s + " "*(20 - key.length) + format("%.4f", [value,0.0].max ) + "\n";
}
fout.close